<template>
  <div class="list_item" :title="dynamic.content">
    <div class="main_container">
      <div class="left_box">
        <a
          :href="`//space.bilibili.com/${dynamic.uid}/dynamic`"
          target="_blank"
          class="avatar"
          :style="{ backgroundImage: `url(${dynamic.avatar})` }"
        ></a>
        <div
          class="verify_box"
          v-if="dynamic.utype != null || dynamic.vip"
          :class="[
            dynamic.utype == '0'
              ? 'type_0'
              : dynamic.utype == '1'
              ? 'type_1'
              : '',
            dynamic.vip ? 'type_big' : '',
          ]"
        ></div>
      </div>
      <div class="center_box">
        <div class="name_line">
          <a
            :href="`//space.bilibili.com/${dynamic.uid}/dynamic`"
            :title="dynamic.uname"
            class="user_name"
            target="_blank"
            >{{ dynamic.uname }}</a
          >
          <span class="publish_time">{{
            dynamic.publishTime | timeFilter
          }}</span>
        </div>
        <a
          :href="`//www.bilibili.com/video/${dynamic.videoId}`"
          :title="dynamic.content"
          class="content"
          target="_blank"
          >{{ dynamic.content }}</a
        >
      </div>
      <a
        class="right_box"
        :href="`//www.bilibili.com/video/${dynamic.videoId}`"
        target="_blank"
      >
        <div
          class="cover"
          :style="{ backgroundImage: `url(${dynamic.poster})` }"
        >
          <div
            class="watch_later"
            @click.prevent="addWatchLater"
            @mouseenter="showTip"
            @mouseleave="hiddenTip"
          >
            <div class="watch_later_tip">{{ tipContent }}</div>
          </div>
        </div>
      </a>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tipContent: "",
    };
  },
  props: {
    dynamic: {
      typeof: Object,
      required: true,
    },
  },
  methods: {
    addWatchLater() {
      if (this.dynamic.watchLater) {
        this.$set(this.dynamic, "watchLater", false);
        this.tipContent = "已移除";
      } else {
        this.$set(this.dynamic, "watchLater", true);
        this.tipContent = "已添加稍后再看";
      }
      // 发送取消请求
    },
    showTip() {
      if (this.dynamic.watchLater) {
        this.tipContent = "移除";
      } else {
        this.tipContent = "稍后再看";
      }
    },
    hiddenTip() {
      this.tipContent = "";
    },
  },
  filters: {
    timeFilter(date) {
      let dateTime = new Date(date);
      let dateTN = dateTime.getTime();
      let dateMonth = dateTime.getMonth() + 1;
      let dateDate = dateTime.getDate();
      let dateHour = dateTime.getHours();
      let dateMinute = dateTime.getMinutes();

      let nowTime = new Date();
      let nowTN = nowTime.getTime();
      let nowHour = nowTime.getHours();
      let nowMinute = nowTime.getMinutes();
      let las = Math.ceil((nowTN - dateTN) / 1000);

      if (las < 60) {
        return "刚刚";
      } else if (las < 60 * 60) {
        return `${nowMinute - dateMinute} 分钟前`;
      } else if (las < 60 * 60 * 24) {
        return `${nowHour - dateHour} 小时前`;
      } else if (las < 60 * 60 * 24 * 2) {
        return `昨天 ${dateHour}:${dateMinute}`;
      } else {
        return `${dateMonth}-${dateDate}`;
      }
    },
  },
};
</script>

<style lang="scss">
.list_item {
  padding: 11px 19px;
  display: flex;
  flex-direction: column;
  cursor: pointer;
  transition: all 0.3s;
  .main_container {
    display: flex;
    flex-direction: row;
    .left_box {
      width: 34px;
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
      align-items: center;
      position: relative;
      .avatar {
        width: 34px;
        height: 34px;
        background-color: #f4f4f4;
        background-size: cover;
        background-position: center;
        background-repeat: no-repeat;
        display: block;
        border-radius: 50%;
      }
    }
    .center_box {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
      align-items: flex-start;
      padding: 0 11px;
      .name_line {
        display: inline-block;
        font-size: 12px;
        color: #505050;
        line-height: normal;
        text-align: left;
        .user_name {
          display: inline-block;
        }
        .publish_time {
          margin-left: 10px;
          color: #999;
          display: inline-block;
        }
      }
      .content {
        overflow: hidden;
        text-overflow: ellipsis;
        word-break: break-all;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 3;
        margin-top: 6px;
        font-size: 14px;
        font-weight: 500;
        max-width: 190px;
        text-align: left;
        line-height: normal;
      }
    }
    .right_box {
      width: 77px;
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
      align-items: center;
      position: relative;
      .cover {
        width: 100%;
        height: 34px;
        border-radius: 2px;
        background-color: #f4f4f4;
        background-size: cover;
        background-position: center;
        background-repeat: no-repeat;
        display: flex;
        justify-content: center;
        align-items: center;
        .watch_later {
          position: relative;
          width: 21px;
          height: 21px;
          display: none;
          background-image: url("/static/icon/video/play.png");
          background-size: cover;
          background-position: center;
          background-repeat: no-repeat;
          .watch_later_tip {
            padding: 6px 8px;
            background-color: rgba(0, 0, 0, 0.7);
            font-size: 12px;
            line-height: 12px;
            position: absolute;
            white-space: nowrap;
            top: -26px;
            left: 50%;
            transform: translateX(-50%);
            border-radius: 2px;
            color: #fff;
            display: none;
          }
          &:hover .watch_later_tip {
            display: block;
          }
        }
        &:hover .watch_later {
          display: block;
        }
      }
    }
  }
}
</style>